﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace TCA_User
{
    public partial class TCA_Login : Form
    {
        public TCA_Login()
        {
            InitializeComponent();
            Init_form_Configure();
        }
        DataTable user_info;
        DataSet user_info_dataset = new DataSet();
        SqlDataAdapter user_info_dataAdapter = new SqlDataAdapter();


        private void Login_BT_Click(object sender, EventArgs e)
        {
            string username = UserName_txt.Text.Trim();
            string password;

            username = Map_UserName(username);
            user_info = Get_UserInfo(username, ref user_info_dataAdapter, ref user_info_dataset);
            if (user_info.Rows.Count == 0)
            {
                MessageBox.Show("This Username did not register. \nPlease contact with Admin!");
                return;
            }
            password = ((string)user_info.Rows[0]["password"]).Trim();
            if (Password_txt.Text.Trim() == password)
            {
                //TCA_User.Bill_List.Bill_List bill_list = new TCA_User.Bill_List.Bill_List();
                //bill_list.Show();
                Thread Bill_List_Thread = new Thread(run_Bill_List);
                Bill_List_Thread.SetApartmentState(ApartmentState.STA);
                Bill_List_Thread.Start();
                this.Close();
                // Application.Exit();

            }
            else {
                MessageBox.Show("Wrong Password");
            }
        }

        
        private void run_Bill_List()
        {
            Application.Run(new TCA_User.Bill_List(user_info));
        }

        private void HidePass_check_CheckedChanged(object sender, EventArgs e)
        {
            if (HidePass_check.Checked == true)
            {
                Password_txt.UseSystemPasswordChar = true;
            }
            else 
            {
                Password_txt.UseSystemPasswordChar = false;
            }
        }

        private void Password_txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==13)
            {
                Login_BT_Click(null, null);
            }
        }

    }
}
